package LeetCode;

import com.company.Tree.Tree;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.util.ArrayList;
import java.util.List;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}14:17
 */
public class LeetCode94 {
    //144. 二叉树的前序遍历
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
 }

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        helper(root,list);
        return list;
    }

    public void helper(TreeNode node,List<Integer> list){
        if (node != null){
            list.add(node.val);
            if (node.left != null){
                helper(node.left,list);
            }
            if (node.right!=null){
                helper(node.right,list);
            }
        }
    }

    //94. 二叉树的中序序遍历
    public void helper2(TreeNode node,List<Integer> list){
        if (node != null){
            if (node.left != null){
                helper(node.left,list);
            }
            list.add(node.val);
            if (node.right!=null){
                helper(node.right,list);
            }
        }
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        inOrderHelper(root,list);
        return list;
    }
    public void inOrderHelper(TreeNode node,List<Integer> list){
        if (node != null){
            inOrderHelper(node.left,list);
            list.add(node.val);
            inOrderHelper(node.right,list);
        }
    }
}
